package com.storage.web.controller.system;

import com.storage.common.annotation.Log;
import com.storage.common.core.controller.BaseController;
import com.storage.common.core.domain.AjaxResult;
import com.storage.common.core.domain.ServiceResponse;
import com.storage.common.core.page.TableDataInfo;
import com.storage.common.enums.BusinessType;
import com.storage.framework.web.OperationContext;
import com.storage.framework.web.enums.RequestOperation;
import com.storage.system.domain.StorageUser;
import com.storage.system.service.IStoragePropertiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 属性 信息操作处理
 *
 * @author storage
 * @date 2019-08-18
 */
@Api(tags = "属性相关接口")
@RestController
public class StoragePropertiesController extends BaseController {

    @Autowired
    private IStoragePropertiesService storagePropertiesService;

    /**
     * 查询属性列表
     */
    @ApiOperation(value = "查询属性列表")
    @GetMapping("/system/storageProperties/list")
    @ResponseBody
    public Object list(@RequestOperation OperationContext context,
                       @RequestParam("categoryId") Long categoryId,
                       @RequestParam(value = "pageNum", required = false) Integer pageNum,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        startPage();
        StorageUser currentUser = context.getStorageUser();
        ServiceResponse<TableDataInfo> response = storagePropertiesService.selectStoragePropertiesList(currentUser, categoryId, pageNum, pageSize);
        if (!response.isSuccess()) {
            return AjaxResult.error(response.getError());
        }
        return response.getData();
    }

    /**
     * 新增/修改保存属性
     */
    @ApiOperation(value = "新增/修改保存属性")
    @Log(title = "属性", businessType = BusinessType.UPDATE)
    @PostMapping("/system/storageProperties/save/{category_id}")
    @ResponseBody
    public AjaxResult editSave(@RequestOperation OperationContext context,
                               @RequestBody String storageProperties,
                               @PathVariable("category_id") Long categoryId) {
        StorageUser currentUser = context.getStorageUser();
        return storagePropertiesService.updateStorageProperties(currentUser, storageProperties, categoryId);
    }

}
